package baiqi.sishen.community.controller;

import baiqi.sishen.community.cache.TagsCache;
import baiqi.sishen.community.dto.QuestionDTO;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.model.Question;
import baiqi.sishen.community.model.User;
import baiqi.sishen.community.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/4/28
 */
@Controller
public class PublishController {

    @Resource
    private QuestionService questionService;


    @GetMapping("/publish")
    public String publish(Model model) {
        //返回标签库
        model.addAttribute("tags", TagsCache.get());
        //选中标识符
        model.addAttribute("navType","publishNav");
        return "publish";
    }

    /**
     * 问题的提交
     *
     * @param title       问题题目
     * @param description 问题内容
     * @param tag         问题标签
     * @param id          问题id
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/publish")
    public String publish(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "tag", required = false) String tag,
            @RequestParam(value = "id", required = false) Long id,
            HttpServletRequest request, Model model) {
        //数据回显
        model.addAttribute("title", title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);
        //返回标签库
        model.addAttribute("tags", TagsCache.get());

        if (title == null || title.equals("")) {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        //WangEditor编辑的默认字符
        String defaultDescription = "<p id=\"descriptionP\"></p><p><br></p>";
        if (description == null || description.equals(defaultDescription)) {
            model.addAttribute("error", "内容不能为空");
            return "publish";
        }
        if (tag == null || tag.equals("")) {
            model.addAttribute("error", "标签不能为空");
            return "publish";
        }

        String filterInvalid = TagsCache.filterInvalid(tag);
        if (StringUtils.isNotBlank(filterInvalid)) {
            model.addAttribute("error", "输入非法标签:"+filterInvalid);
            return "publish";
        }

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            model.addAttribute("error", "亲 先登录");
            return "publish";
        }
        Question question = new Question();
        //去除题目前后空格
        question.setTitle(title.trim());
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setId(id);
        //增加或更新数据
        questionService.insertOrUpdate(question);
        return "redirect:/";
    }

    /**
     * 获取问题数据 进行回显
     *
     * @param id    问题id
     * @param model model
     * @return publish
     */
    @GetMapping("/publish/{id}")
    public String updateQuestion(@PathVariable("id") Long id, Model model) {
        //选中标识符
        model.addAttribute("navType","publishNav");

        QuestionDTO questionDTO = questionService.getQuestionById(id);
        if (questionDTO == null) {
            throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
        }
        model.addAttribute("title", questionDTO.getTitle());
        model.addAttribute("description", questionDTO.getDescription());
        model.addAttribute("tag", questionDTO.getTag());
        model.addAttribute("id", questionDTO.getId());
        //返回标签库
        model.addAttribute("tags", TagsCache.get());
        return "publish";
    }
}
